package handlers

import (
	"net/http"

	"gitee.com/daonna/video-copyright-blockchain/internal/services"
	"github.com/gin-gonic/gin"
)

type VideoHandler struct {
	videoService *services.VideoService
}

func NewVideoHandler(vs *services.VideoService) *VideoHandler {
	return &VideoHandler{
		videoService: vs,
	}
}

func (vh *VideoHandler) CreateVideo(c *gin.Context) {
	var input struct {
		Hash string `json:"hash" binding:"required"`
	}

	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	if err := vh.videoService.AddVideoHash(input.Hash); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to add video hash"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Video hash added to blockchain"})
}

func (vh *VideoHandler) VerifyVideo(c *gin.Context) {
	hash := c.Query("hash")
	if hash == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Hash parameter is required"})
		return
	}

	isVerified := vh.videoService.VerifyVideoHash(hash)
	c.JSON(http.StatusOK, gin.H{"verified": isVerified})
}
